#!/bin/bash

monitor_cfl(){
grep "Courant Number mean" $2 | grep -v "Interface Courant Number mean" | cut -d ":" -f3 > max_co_${1}.txt   
}

get_time(){
grep -w "Time =" $2 | cut -d "=" -f2 > time_${1}.x
}

cd damBreak
./Allclean
ls
cd ..

src_case=damBreak
deltaT="0.001 0.002 0.003 0.007"
# deltaT="0.001"

for num in $deltaT
do
    fileName=${src_case}_${num}
    if [ -d $fileName ]
    then
        echo "file $fileName already exist"
    else
        echo "create case:  $fileName"
        cp -r $src_case $fileName
    fi

    cd $fileName
    ./Allclean
    # 1. OpenFOAM's embeded script
    foamDictionary system/controlDict -entry deltaT -set $num
    #foamDictionary system/controlDict -entry stopAt -set writeNow
    #foamDictionary system/controlDict -entry endTime -set 2222
    
    # 2. Use `sed`
    # 这样不会把修改后的值自己打出来，foamDictionary会
    # 一般ICNS用下面这种，OpenFOAM就用foamDictionary
    #para_name=stopAt
    #para_value=tttTest
    #sed -i '' -E "s|^([[:space:]]*[[:space:]]*$para_name[[:space:]]*)[0-9a-zA-Z]+|\1$para_value|" system/controlDict
    ./Allrun


    # 后处理
    monitor_cfl $num log.interFoam
    get_time $num log.interFoam
    cd ..

done
